from django.contrib.auth.backends import ModelBackend
import re
from users.models import User


def get_user_by_acount(username):
    """
    :param username: 用户登录传进来的用户名
    :return: 存在数据库中的用户名
    """
    try:
        if re.match(r'^1[3-9]\d{9}$', username):
            # 手机号登录
            user = User.objects.get(mobile=username)
        else:
            # 用户名登录
            user = User.objects.get(username=username)
    except User.DoesNotExist:
        return None
    else:
        return user


class UserNameModelBackend(ModelBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):
        # 判断用户是否通过vue组件发送
        if request is None:
            # 后台管理登录
            try:
                # is_superuser判断用户是否是超级管理员用户, 如果用户没有这个值，则会user=none
                user = User.objects.get(username=username, is_superuser=True)
            except:
                return None
            # 判断密码
            if user is not None and user.check_password(password):
                return user
        else:
            # 用户登录
            # 获取用户登录名信息
            user = get_user_by_acount(username)
            # 检查用户和登录名是否存在
            if user and user.check_password(password):
                return user
